package com.woniu.defecttracking.controller.bugs;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.defecttracking.commens.ComUtil;
import com.woniu.defecttracking.commens.Result;
import com.woniu.defecttracking.dto.AddWorkLogDto;
import com.woniu.defecttracking.dto.GetWorkHoursByDayDto;
import com.woniu.defecttracking.dto.GetWorkLogByMonthDto;
import com.woniu.defecttracking.entity.AccountDetails;
import com.woniu.defecttracking.entity.WorkLog;
import com.woniu.defecttracking.exception.WorkLogException;
import com.woniu.defecttracking.param.AddWorkLogParam;
import com.woniu.defecttracking.param.BasePaperParam;
import com.woniu.defecttracking.param.GetWorkLogByDayParam;
import com.woniu.defecttracking.param.GetWorkLogByMonthParam;
import com.woniu.defecttracking.service.impl.WorkLogServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 李东辉
 * @since 2021-12-31
 */
@RestController
@RequestMapping("/workLog")
public class WorkLogController {

    @Autowired
    private WorkLogServiceImpl workLogService;


    /*
     *
     *日志的增加
     *
     */


    /*
     *查 统计时间、人 关联时间、项目查
     *查日报 查月报  根据bugid查总共工时
     * */

    /**
     * @作者: lidonghui
     * @描述：根据每一条缺陷的操作记录[反馈、新增、修改、删除、分配、处理]生成一条日志记录
     * @时间：2021/12/31 16:52
     * @参数：[]
     * @参数类型:
     * @返回：com.woniu.defecttracking.commens.Result
     **/
    @PostMapping("/addWorkLog")
    @ResponseBody
    public Result addWorkLog(@RequestBody AddWorkLogParam addWorkLogParam) throws WorkLogException {

        Result result = new Result();
        //这里查询当前登录人信息，替换1
        AccountDetails principal = (AccountDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer accountId = principal.getAccountId();

        try{

        WorkLog workLog = ComUtil.copyProperties(addWorkLogParam, WorkLog.class);
        workLog.setModifyPerson(accountId+"");

        int insert = workLogService.getBaseMapper().insert(workLog);
        AddWorkLogDto addWorkLogDto = ComUtil.copyProperties(workLog, AddWorkLogDto.class);

        if (insert > 0) {
            result.setCode(200);
            result.setMessage("新增成功");
            result.setData(addWorkLogDto);
        } else {
            throw new WorkLogException("数据库新增日志记录失败", 1070);
        }
        }catch (WorkLogException e){
            throw new WorkLogException( e.getMessage(),1070);
        }catch (Exception e){
            throw new WorkLogException("生成日志记录失败",1070);
        }


        return result;
    }


    /**
     * @作者: lidonghui
     * @描述：根据bugid查询总工时数
     * @时间：2022/1/4 23:03
     * @参数：[bugId]
     * @参数类型:
     * @返回：com.woniu.defecttracking.commens.Result
     **/
    @GetMapping("/getTotalHoursByBugId/{bugId}")
    public Result getTotalHoursByBugId(@PathVariable Integer bugId) {
        Result result = new Result();
        Double hours = workLogService.getBaseMapper().selectTotalHoursByBugId(bugId);
        if (hours != null) {
            result.setCode(200);
            result.setMessage("查询成功");
            result.setData(hours);
        }
        return result;
    }

    /**
     * @作者: lidonghui
     * @描述：查询所有日报
     * @时间：2022/1/4 23:04
     * @参数：[basePaperParam]
     * @参数类型:
     * @返回：com.woniu.defecttracking.commens.Result
     **/
    @PostMapping("/getAllByDay")
    public Result getAll(@RequestBody BasePaperParam basePaperParam) {
        Page<GetWorkHoursByDayDto> page = new Page<>(basePaperParam.getPageIndex(), basePaperParam.getPageSize());
        IPage<GetWorkHoursByDayDto> listIPage = workLogService.getBaseMapper().selectAllWorkLogs(page);
        if (listIPage != null) {
            return Result.getInstance(List.class).setData(listIPage).setMessage("查询成功").setCode(200);
        }
        return Result.getInstance(Object.class).setData(null).setMessage("查询失败").setCode(500);
    }

    /**
     * @作者: lidonghui
     * @描述：输入项目名称，登记时间范围，返回对应日报
     * @时间：2022/1/2 16:16
     * @参数：[getWorkLogByDayParam]
     * @参数类型:
     * @返回：com.woniu.defecttracking.commens.Result
     **/
    @PostMapping("/getAllByDayAndKeys")
    public Result getAllByDayAndKeys(@RequestBody GetWorkLogByDayParam getWorkLogByDayParam) {
        Page<GetWorkHoursByDayDto> page = new Page<>(getWorkLogByDayParam.getPageIndex(), getWorkLogByDayParam.getPageSize());
        IPage<GetWorkHoursByDayDto> listIPage = workLogService.getBaseMapper().selectAllByDayAndKeys(page, getWorkLogByDayParam);
        if (listIPage != null) {
            return Result.getInstance(List.class).setData(listIPage).setMessage("查询成功").setCode(200);
        }
        return Result.getInstance(Object.class).setData(null).setMessage("查询失败").setCode(500);
    }

    /**
     * @作者: lidonghui
     * @描述：查询所有月报
     * @时间：2022/1/4 23:04
     * @参数：[basePaperParam]
     * @参数类型:
     * @返回：com.woniu.defecttracking.commens.Result
     **/
    @PostMapping("/getAllByMonth")
    public Result getAllByMonth(@RequestBody BasePaperParam basePaperParam) {
        Page<GetWorkLogByMonthDto> page = new Page<>(basePaperParam.getPageIndex(), basePaperParam.getPageSize());
        IPage<GetWorkLogByMonthDto> listIPage = workLogService.getBaseMapper().selectAllWorkLogsMonthes(page);
        if (listIPage != null) {
            return Result.getInstance(List.class).setData(listIPage).setMessage("查询成功").setCode(200);
        }
        return Result.getInstance(Object.class).setData(null).setMessage("查询失败").setCode(500);
    }

    /**
     * @作者: lidonghui
     * @描述：根据登记时间查询月报信息
     * @时间：2022/1/2 17:52
     * @参数：[]
     * @参数类型:
     * @返回：com.woniu.defecttracking.commens.Result
     **/
    @PostMapping("/getAllByMonthAndKeys")
    public Result getAllByMonthAndKeys(@RequestBody GetWorkLogByMonthParam getWorkLogByMonthParam) {
        String createTimeStart = getWorkLogByMonthParam.getCreateTimeStart();
        String createTimeEnd = getWorkLogByMonthParam.getCreateTimeEnd();
        System.out.println("createTimeStart============="+createTimeStart);
        System.out.println("createTimeEnd=================="+createTimeEnd);

        Page<GetWorkLogByMonthDto> page = new Page<>(getWorkLogByMonthParam.getPageIndex(), getWorkLogByMonthParam.getPageSize());
        IPage<GetWorkLogByMonthDto> listIPage = workLogService.getBaseMapper().selectAllByMonthAndKeys(page, getWorkLogByMonthParam);
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("ipage1",listIPage);
        objectObjectHashMap.put("ipage2",listIPage);
        if (listIPage != null) {
            return Result.getInstance(List.class).setData(objectObjectHashMap).setMessage("查询成功").setCode(200);
        }
        return Result.getInstance(Object.class).setData(null).setMessage("查询失败").setCode(500);
    }

    /**
    * @作者: lidonghui
    * @描述：根据bugId获取所有的工作日报
    * @时间：2022/1/5 13:58
    * @参数：[bugid]
    * @参数类型:
    * @返回：com.woniu.defecttracking.commens.Result
    **/
    @GetMapping("/getWorkLogListByBUgId/{bugid}")
    public Result getWorkLogListByBUgId(@PathVariable("bugid") Integer bugid) throws WorkLogException {
        Result result = new Result();

        List<WorkLog> list= workLogService.getBaseMapper().selectListByBugId(bugid);
        if(null!=list){
            result.setCode(200);
            result.setMessage("查询成功");
            result.setData(list);
        }else {
            throw new WorkLogException("查询数据库失败",1071);
        }

        return result;
    }

}

